
import time
from . import ConnPG
from setting import TOKEN_VALID_TIME

class VerfiyToken:

    async def verify_token(self, user_id, token):

        if not all([user_id, token]):

            return {
                'status': -1,
                'errorCode': 100104,
                'errorMsg': 'user_id或token值为空',
                'resultBody': None
            }
        
        sql = 'select * from token where user_id=%s and token=%s'


        result = await ConnPG().fetchone(sql, (user_id, token))

        if result == None:

            return {
                'status': -1,
                'errorCode': 100105,
                'errorMsg': '没有找到token值，请检查token值或user_id是否正确',
                'resultBody': None
            }
        
        elif result['token_time'] + TOKEN_VALID_TIME <= time.time():#token值过期

            return {
                'status': -1,
                'errorCode': 100106,
                'errorMsg': 'token值过期，请重新登录',
                'resultBody': None
            }

        else:

            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': dict(result)
            }